package sicau.carManager.Util.LicensePlateRecognitionUtil;

import lombok.extern.slf4j.Slf4j;

import java.net.URLEncoder;

/**
 * @Author ITcz
 * @Data 2021-01-30 - 11:12
 * * 车牌识别服务，调用licensePlate方法传入需要识别的图片url地址，返回识别后的车牌号
 */
@Slf4j
public class LicensePlateRecognition {

    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
    /**
     * 根据图片路径识别车牌
     * */
    public static String licensePlateByPath(String imagePath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";
        try {
            // 本地文件路径
            String filePath = imagePath;
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam;

            // 注意这里每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            if (AuthService.TOKEN == "") {
                AuthService.TOKEN = AuthService.getAuth();
                System.out.println("初始化车牌识别的token...");
            }
            String accessToken = AuthService.TOKEN;

            String result = HttpUtil.post(url, accessToken, param);
            //从返回结果中取出车牌号
            log.info("调用百度车牌识别接口返回结果：" + result.substring(27,34));
            return result.substring(27,34);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
    * 根据图片二进制流识别车牌
    * */
    public static String licensePlateByByte(byte[] imageData) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";
        try {
            String imgStr = Base64Util.encode(imageData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam;

            // 注意这里每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            if (AuthService.TOKEN == "") {
                AuthService.TOKEN = AuthService.getAuth();
                System.out.println("初始化车牌识别的token...");
            }
            String accessToken = AuthService.TOKEN;

            String result = HttpUtil.post(url, accessToken, param);
            //从返回结果中取出车牌号
            if (result.contains("error")) {
                return "车牌识别失败";
            }
            return result.substring(27,34);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

 /*   public static void main(String[] args) {
        LicensePlateRecognition.licensePlateByPath("C:\\Users\\Alice\\Desktop\\3.jpg");
        LicensePlateRecognition.licensePlateByPath("C:\\Users\\Alice\\Desktop\\4.jpg");
    }*/
}